from django.shortcuts import render_to_response
from django.http import HttpResponse, Http404
import datetime
from books.models import Author

def root (request):
    uri_paths = [
        (r"/admin/",) * 2,
        (r"/hello/",) * 2,
        (r"/time/",) * 2,
        (r"/time/plus/(\d{1,3})/", "/time/plus/2/"),
        (r"/authors/",) * 2,
    ]
    return render_to_response("root.html", locals())

def hello(request):
    return render_to_response("hello.html")

def current_datetime(request):
    current_date = datetime.datetime.now()
    return render_to_response("current_datetime.html", locals())

def hours_ahead(request, offset_str):
    try:
        hour_offset = int(offset_str)
    except ValueError:
        raise Http404()
    future_time = datetime.datetime.now() + datetime.timedelta(hours=hour_offset)
    return render_to_response("hours_ahead.html", locals())

def authors (request):
    num_authors = Author.objects.count()
    authors = Author.objects.order_by("last_name", "first_name")
    return render_to_response("authors.html", locals())
